package com.mwj.gulimall.prodict.service.impl;

import com.mwj.gulimall.prodict.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mwj.common.utils.PageUtils;
import com.mwj.common.utils.Query;

import com.mwj.gulimall.prodict.dao.CategoryDao;
import com.mwj.gulimall.prodict.entity.CategoryEntity;
import com.mwj.gulimall.prodict.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWIthTrre() {
        //1.查询所有的分类
        List<CategoryEntity> category = baseMapper.selectList(null);
        //2.查询一级分类
        List<CategoryEntity> leve1Menus = category.stream().filter((categoryEntity) ->
             categoryEntity.getParentCid() == 0
        ).map((meun)->{
            meun.setChildren(getChildrens(meun,category));
            return meun;
        }).sorted((muen1, muen2) -> {
            return (muen1.getSort()==null?0:muen1.getSort()) -  (muen2.getSort()==null?0:muen2.getSort());
        }).collect(Collectors.toList());
        return leve1Menus;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        //TODO 检查当前菜单是否被引用
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[]  findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId,paths);
        Collections.reverse(parentPath);
        return  parentPath.toArray(new Long[parentPath.size()]);
    }

    /*
    级联更新所有关联的数据
     */
    @Transactional
    @Override
    public void updateCascade(CategoryEntity category) {
         this.updateById(category);//跟新自己
         categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());//跟新关联表

    }

    private List<Long> findParentPath(Long catelogId,List<Long> paths){
        paths.add(catelogId);//保存当前节点id
        CategoryEntity byId = this.getById(catelogId);
        if (byId.getParentCid()!=0){//招父节点
            findParentPath(byId.getParentCid(),paths);
        }
        return paths;
    }

    //递归查询子菜单
    private List<CategoryEntity>getChildrens(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map(categoryEntity -> {
            //找到子菜单
            categoryEntity.setChildren(getChildrens(categoryEntity, all));
            return categoryEntity;
        }).sorted((muen1, muen2) -> {
            //排序
            return (muen1.getSort()==null?0:muen1.getSort()) -  (muen2.getSort()==null?0:muen2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

}